/**
 * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved.
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package easemob.chatuidemo.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.Toast;

import com.easemob.chat.EMGroupManager;
import com.easemob.exceptions.EaseMobException;
import com.wolfalpha.utron.R;
import com.wolfalpha.utron.activity.base.BaseNormal3Activity;
import com.wolfalpha.utron.view.AlertDialog;

public class NewGroupActivity extends BaseNormal3Activity {
    private EditText groupNameEditText;
    private ProgressDialog progressDialog;
    private EditText introductionEditText;
    private EditText maxEdit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_partner_new_jlq);
        groupNameEditText = (EditText) findViewById(R.id.edit_jlq_name);
        introductionEditText = (EditText) findViewById(R.id.edit_jlq_introduction);
        maxEdit = (EditText) findViewById(R.id.edit_jlq_max);

        setTvTitle("新建讨论组");
    }

    @Override
    public void rightOperation() {
        save();
    }

    /**
     * 保存群组信息并进入选择
     */
    public void save() {
        String str6 = "讨论组名称不能为空";
        String name = groupNameEditText.getText().toString();
        if (TextUtils.isEmpty(name)) {
            AlertDialog ad = new AlertDialog(this);
            ad.setTitle("提示");
            ad.setMessage(str6);
            ad.setPositiveButton("确认");
            ad.setOnlyPositive();
        } else {
            // 进通讯录选人
            startActivityForResult(new Intent(this, GroupPickContactsActivity.class).putExtra("groupName", name), 0);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        String st1 = "正在创建中...";
        final String st2 = "创建群失败";
        if (resultCode == RESULT_OK) {
            //新建群组
            progressDialog = new ProgressDialog(this);
            progressDialog.setMessage(st1);
            progressDialog.setCanceledOnTouchOutside(false);
            progressDialog.show();

            new Thread(new Runnable() {
                @Override
                public void run() {
                    // 调用sdk创建群组方法
                    String groupName = groupNameEditText.getText().toString().trim();
                    String desc = introductionEditText.getText().toString();
                    int max = 20;
                    if (!TextUtils.isEmpty(maxEdit.getText().toString())) {
                        max = Integer.valueOf(maxEdit.getText().toString());
                    }

                    if (TextUtils.isEmpty(desc)) {
                        desc = "这是一个讨论组";
                    }
                    String[] members = data.getStringArrayExtra("newmembers");
                    try {
                        //创建不公开群
                        //最大人数 50个
                        EMGroupManager.getInstance().createPublicGroup(groupName, desc, members, true, max);
                        runOnUiThread(new Runnable() {
                            public void run() {
                                progressDialog.dismiss();
                                setResult(RESULT_OK);
                                finish();
                            }
                        });
                    } catch (final EaseMobException e) {
                        runOnUiThread(new Runnable() {
                            public void run() {
                                progressDialog.dismiss();
                                Toast.makeText(NewGroupActivity.this, "创建失败:" + e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
                            }
                        });
                    }

                }
            }).start();
        }
    }

}
